package com.qrcode.zbar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;

import androidx.lifecycle.LifecycleOwner;

import cn.bingoogolapple.qrcode.core.BGAQRCodeUtil;

/**
 * 项目名称：com.example.Zbar.utils
 * 创建人:LuoFG
 * 创建时间:2021/9/6  11:37
 * 描述:
 * 作用:
 */
public class ZbarClient {
    
    private static class InstanceHolder {
        private static ZbarClient instance = new ZbarClient();
    }

    private static ZbarClient getInstance() {
        return InstanceHolder.instance;
    }

    public static ZbarClient getClient() {
        return getInstance();
    }

    /***配置资源-Start***/
    //是否显示左边返回键文字
    private boolean isShowBackText = false;
    //相册功能是否开放
    private boolean isOpenPhoto = false;
    //默认Bar文字白色
    private int barTextColor = Color.WHITE;
    //默认Bar背景白色
    private int barBgColor = Color.BLUE;
    //默认Bar返回键
    private int imgBack = R.mipmap.ic_arrow_scan_back;

    //是否自动缩放
    private boolean isAutoZoom = false;
    //是否是条形码框 true为条形码
    private boolean isBarcode = false;
    //扫描框边框默认颜色  白色
    private int scanBorderColor = Color.WHITE;
    //扫描框边角线默认颜色（折角）  白色
    private int scanCornerColor = Color.WHITE;
    //扫描线默认颜色  白色
    private int scanLineColor = Color.WHITE;
    //扫描提示默认颜色  白色
    private int scanTipTextColor = Color.WHITE;
    //扫描提示
    private String scanTipText = "将二维码/条码放入框内，即可自动扫描";
    //Bar返回提示
    private String barBackText="返回";
    //Bar标题提示
    private String barTitleText="扫一扫";
    /***配置资源-End***/
    //传入具体包名类  作为区分
    private String ZbarTag="";
    //标题栏颜色是否沉浸到状态栏
    private boolean isToOsBar = false;

    //设置成为默认属性
    public ZbarClient setDefault() {
        //是否显示左边返回键文字
        isShowBackText = false;
        //相册功能是否开放
        isOpenPhoto = false;
        //是否自动缩放
        isAutoZoom = true;
        //默认Bar文字白色
        barTextColor = Color.WHITE;
        //默认Bar背景白色
        barBgColor = Color.BLUE;
        //默认Bar返回键
        imgBack = R.mipmap.ic_arrow_scan_back;

        //是否是条形码框 true为条形码
        isAutoZoom = false;
        //是否是条形码框 true为条形码
        isBarcode = false;
        //扫描框边框默认颜色  白色
        scanBorderColor = Color.WHITE;
        //扫描框边角线默认颜色（折角）  白色
        scanCornerColor = Color.WHITE;
        //扫描线默认颜色  白色
        scanLineColor = Color.WHITE;
        //扫描提示默认颜色  白色
        scanTipTextColor = Color.WHITE;
        //扫描提示
        scanTipText = "将二维码/条码放入框内，即可自动扫描";
        barBackText="返回";
        barTitleText="扫一扫";
        isToOsBar=false;

        return this;
    }

    public interface ScanResult{
        void back(ZbarResultBean result);
    }

    public ZbarClient setZbarResultListener(Activity activity, ScanResult scanResult){
        try {
            ZbarCodeActivity.qrcodeZbarResult.observe((LifecycleOwner) activity, s -> {
                //Log.e("收到的识别回调",s.getTag()+s.getResult());
                scanResult.back(s);
                ZbarCodeActivity.qrcodeZbarResult.removeObservers((LifecycleOwner) activity);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }


    /**
     * @param activity 启动Zbar识别
     */
    public void startZbar(Activity activity){
        activity.startActivity(new Intent(activity, ZbarCodeActivity.class));
    }

    public ZbarClient setDebug(boolean debug){
        BGAQRCodeUtil.setDebug(debug);
        return this;
    }

    public ZbarClient setToOsBar(boolean toOsBar){
        isToOsBar=toOsBar;
        return this;
    }

    public boolean isToOsBar() {
        return isToOsBar;
    }

    public String getZbarTag() {
        return ZbarTag;
    }

    public ZbarClient setZbarTag(String ZbarTag) {
        ZbarTag = ZbarTag;
        return this;
    }

    public boolean isAutoZoom() {
        return isAutoZoom;
    }

    public ZbarClient setAutoZoom(boolean autoZoom) {
        isAutoZoom = autoZoom;
        return this;
    }

    public boolean isBarcode() {
        return isBarcode;
    }

    public ZbarClient setBarcode(boolean barcode) {
        isBarcode = barcode;
        return this;
    }

    public int getScanCornerColor() {
        return scanCornerColor;
    }

    /**
     * @param scanCornerColor  此方法暂时不生效
     * @return
     */
    public ZbarClient setScanCornerColor(int scanCornerColor) {
        this.scanCornerColor = scanCornerColor;
        return this;
    }

    public String getBarTitleText() {
        return barTitleText;
    }

    public ZbarClient setBarTitleText(String barTitleText) {
        this.barTitleText = barTitleText;
        return this;
    }

    public String getBarBackText() {
        return barBackText;
    }

    public ZbarClient setBarBackText(String barBackText) {
        this.barBackText = barBackText;
        return this;
    }

    public boolean isShowBackText() {
        return isShowBackText;
    }

    public ZbarClient setShowBackText(boolean showBackText) {
        isShowBackText = showBackText;
        return this;
    }

    public boolean isOpenPhoto() {
        return isOpenPhoto;
    }

    public ZbarClient setOpenPhoto(boolean openPhoto) {
        isOpenPhoto = openPhoto;
        return this;
    }

    public int getBarTextColor() {
        return barTextColor;
    }

    public ZbarClient setBarTextColor(int barTextColor) {
        this.barTextColor = barTextColor;
        return this;
    }

    public int getBarBgColor() {
        return barBgColor;
    }

    public ZbarClient setBarBgColor(int barBgColor) {
        this.barBgColor = barBgColor;
        return this;
    }

    public int getImgBack() {
        return imgBack;
    }

    public ZbarClient setImgBack(int imgBack) {
        this.imgBack = imgBack;
        return this;
    }

    public int getScanBorderColor() {
        return scanBorderColor;
    }

    public ZbarClient setScanBorderColor(int scanBorderColor) {
        this.scanBorderColor = scanBorderColor;
        return this;
    }

    public int getScanLineColor() {
        return scanLineColor;
    }

    public ZbarClient setScanLineColor(int scanLineColor) {
        this.scanLineColor = scanLineColor;
        return this;
    }

    public int getScanTipTextColor() {
        return scanTipTextColor;
    }

    public ZbarClient setScanTipTextColor(int scanTipTextColor) {
        this.scanTipTextColor = scanTipTextColor;
        return this;
    }

    public String getScanTipText() {
        return scanTipText;
    }

    public ZbarClient setScanTipText(String scanTipText) {
        this.scanTipText = scanTipText;
        return this;
    }



}
